﻿using Hetang.Areas.User.Models.Classes;
using Hetang.Models.DataContext;
using Hetang.Models.Helper;
using Hetang.Models.Interface;
using Hetang.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hetang.Areas.User.Models.Helper
{
    public class ResumeHelper
    {
        public dt_Resume HandleResume(dt_Resume person, string[] EnglishList)
        {
            IConfigRepository _cr = new ConfigRepository();

            if (person.BornYear < 1980 && person.BornYear >= DateTime.Now.Year - 18)
                person.BornYear = 1980;
            if (person.BornMonth <= 0 && person.BornMonth > 12)
                person.BornMonth = 1;
            if (DateTime.IsLeapYear(Convert.ToInt32(person.BornYear)))
            {
                if (person.BornDay > 29 && person.BornDay <= 0)
                    person.BornDay = 1;
            }
            else
            {
                if(person.BornDay >28 && person.BornDay <=0)
                    person.BornDay = 1;
            }

            person.resume.Born = DateTime.Parse(person.BornYear + "-" + person.BornMonth + "-" + person.BornDay);

            List<dt_Domicile_City> dt_city = _cr.GetResumeDomicile();
            dt_Resume_Domicile dom = new dt_Resume_Domicile();
            foreach (var i in dt_city)
            {
                if (i.name.Equals(person.city))
                {
                    dom.city = i.name;
                    foreach (var j in i.townList)
                    {
                        if (j.name.Equals(person.town))
                        {
                            dom.town = j.name;
                            break;
                        }
                        else
                        {
                            dom.town = "荷塘";
                        }
                    }
                    break;
                }
                else
                {
                    dom.city = "江门";
                }                
            }

            person.resume.Domicile = JsonHelper.stringify(dom);

            if (EnglishList != null)
                person.resume.English = string.Join(" ", EnglishList);
            else
                person.resume.English = string.Empty;

            if (person.resume.English.Length > 80)
                person.resume.English = string.Empty;

            return person;
        }
    }    
}